package com.chushouya.manager.dao.repository;

import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.VisitAreaEntity;
import com.chushouya.manager.dao.mapper.VisitAreaEntityMapper;
import com.chushouya.manager.dto.admin.visitarea.VisitAreaQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Repository
public class VisitAreaRepository extends BaseRepository<VisitAreaEntityMapper, VisitAreaEntity, Long> {

    private Example buildExample(VisitAreaQuery query) {
        final Example example = this.buildExample();
        if (Strings.isNotEmpty(query.getAreaName())) {
            example.and().andEqualTo("areaName", query.getAreaName());
        }
        if (Longs.notNullAndZero(query.getDistrictCode())) {
            example.and().andEqualTo("districtCode", query.getDistrictCode());
        }
        if (Longs.notNullAndZero(query.getCityCode())) {
            example.and().andEqualTo("cityCode", query.getCityCode());
        }
        if (Strings.isNotEmpty(query.getCityName())) {
            example.and().andEqualTo("cityName", query.getCityName());
        }
        example.orderBy("provinceCode").asc();
        return example;
    }

    public PageResult<VisitAreaEntity> selectPage(VisitAreaQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }

    public List<VisitAreaEntity> selectList(VisitAreaQuery query) {
        Example example = this.buildExample(query);
        return this.selectByExample(example);
    }

    public VisitAreaEntity selectOne(VisitAreaQuery query) {
        Example example = this.buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    /**
     * 根据cityCode删除
     */
    public void deleteByCityCode(Long cityCode) {
        VisitAreaQuery query = new VisitAreaQuery();
        query.setCityCode(cityCode);
        Example example = this.buildExample(query);
        this.deleteByExample(example);
    }

}
